package com.example.testrxjava.fragment.article;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.testrxjava.ArticleDetailsActivity;
import com.example.testrxjava.R;
import com.example.testrxjava.adapter.ActicleListAdapter;
import com.example.testrxjava.base.BaseFragment;
import com.example.testrxjava.util.Action;
import com.example.testrxjava.util.ConTract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 项目名称：TestRxjava
 *
 * @Author 郭迎辉
 * Create by 10:51 on 2019/4/19.
 * Describe:
 */
public class ArticleListFragment extends BaseFragment {
    @BindView(R.id.system_tree_Refresh)
    SmartRefreshLayout system_tree_Refresh;
    @BindView(R.id.system_tree_Recyler)
    RecyclerView system_tree_Recyler;
    private static ArticleListFragment instance;
    private int cid;
    private int page=0;
    private List<ArticleListBean.DataBean.DatasBean> dataBeanList;
    private ActicleListAdapter adapter;

    public static ArticleListFragment getInstance(int id) {
        Bundle bundle = new Bundle();
        bundle.putInt(ConTract.KEY_ARTICLE_ID, id);
        instance = new ArticleListFragment();
        instance.setArguments(bundle);

        return instance;
    }
    private void getData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            cid = bundle.getInt(ConTract.KEY_ARTICLE_ID, -1);
            Log.e("cid==",cid+"");
        }
    }
    @Override
    protected void initData() {
        getData();

        system_tree_Refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page=0;
                dataBeanList.clear();
                Action.getInstance().getArticleList(cid+"", page, new ArticleListSucessCallback() {
                            @Override
                            public void systemTreeAcSucess(ArticleListBean bean) {
                                dataBeanList.addAll(bean.getData().getDatas());
                                adapter.notifyDataSetChanged();

                            }

                            @Override
                            public void systemTreeAcFail(String error) {

                            }
                        });
                refreshlayout.finishRefresh();
            }
        });

        system_tree_Refresh.setOnLoadmoreListener(new OnLoadmoreListener() {

            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
              Action.getInstance().getArticleList(cid+"", page, new ArticleListSucessCallback() {
                  @Override
                  public void systemTreeAcSucess(ArticleListBean bean) {
                      dataBeanList.addAll(bean.getData().getDatas());
                      adapter.notifyDataSetChanged();

                  }

                  @Override
                  public void systemTreeAcFail(String error) {

                  }
              });
                refreshlayout.finishLoadmore();
            }
        });


        Action.getInstance().getArticleList(cid + "", page, new ArticleListSucessCallback() {
            @Override
            public void systemTreeAcSucess(ArticleListBean bean) {
                dataBeanList=bean.getData().getDatas();
                if(dataBeanList!=null){
                    system_tree_Recyler.setLayoutManager(new LinearLayoutManager(getActivity()));
                    adapter=new ActicleListAdapter(R.layout.item_home_list,dataBeanList);
                    adapter.openLoadAnimation();
                    system_tree_Recyler.setAdapter(adapter);
                }

                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Log.e("文章作者","文章详情："+dataBeanList.get(position).getAuthor());
                        ArticleDetailsActivity.startActivityForResultByFragment(getActivity(),
                                ArticleListFragment.this,
                                dataBeanList.get(position).getLink(),
                                dataBeanList.get(position).getTitle(),
                                dataBeanList.get(position).getId(),
                                dataBeanList.get(position).isCollect(),
                                ConTract.REQUEST_ARTICLE_ACTIVITY);
                    }
                });

            }

            @Override
            public void systemTreeAcFail(String error) {

            }
        });




    }

    @Override
    protected View initView() {
        View view= LayoutInflater.from(getActivity()).inflate(R.layout.systemtree_fragment,null);
        dataBeanList=new ArrayList<ArticleListBean.DataBean.DatasBean>();


        return view;
    }
}
